<?php
    class ACustomerCare extends CActiveRecord{
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }
        public function getFeedbackSearch($from_date,$to_date,$username,$page,$row_per_page){
            $condition="";
            $from_date=strtotime($from_date);
            if($from_date>0){
                $condition.=" AND create_date >".$from_date;
            }
            $to_date = $to_date." 23:59:59";
            $to_date=strtotime($to_date);
            if($to_date>0){
                $condition.=" AND create_date <".$to_date;
            }
            if(!empty($username)){
                $condition.= " AND username= '$username' ";
            }
            $connect = Yii::app()->db;
            $sql = "SELECT count(id) as count FROM c_feedback WHERE 1 ".$condition."";
            //echo $sql;die;
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();

            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;

            $sql="SELECT * FROM c_feedback WHERE 1 ".$condition." ORDER BY create_date DESC LIMIT ".$first.",".$row_per_page." ";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        } 
        public function getDataSmsLogSearch($from_date,$to_date,$search_type,$page,$row_per_page,$content_type,$name_search){
            $condition="";
            $str_sql="";
            $from_date=strtotime($from_date);
            if($from_date>0){
                $condition.="AND create_date >".$from_date;
            }
            $to_date = $to_date." 23:59:59";
            $to_date=strtotime($to_date);
            if($to_date>0){
                $condition.=" AND create_date <".$to_date;
            }



            switch($search_type){
                case 2:{
                    $userId=APaymentRevenue::getUserId($name_search);
                    if(!empty($userId)){
                        $id= $userId['id'] ; 
                        $condition.=" AND userId=".$id; 
                    }
                    else{
                        $condition.="";
                    }
                    break;
                }
                case 3: {
                   if(!empty($name_search)) {
                       $condition.=" AND serviceNumber=".$name_search." "; 
                   } else{
                       $condition.=" "; 
                   }
                   break; 
                }
                case 4: {
                    if(!empty($name_search)) {
                       $condition.=" AND mobile=".$name_search." "; 
                   } else{
                       $condition.=" "; 
                   }
                   break;
                }
                case 5:{
                    switch($content_type){
                        case 1:{
                            $condition.=" AND type=1 ";break; 
                        }
                        case 2:{
                            $condition.=" AND type=2 ";break;   
                        }
                        case 3:{
                            $condition.=" AND type=3 ";break;   
                        }
                        case 4:{
                            $condition.=" AND type=4 ";break;   
                        }
                        case 5:{
                            $condition.=" AND type=5 ";break;      
                        }
                        case 6:{
                            $condition.=" AND type=6 ";break;      
                        }
                        case 7:{
                            $condition.=" AND type=7 ";break;      
                        }
                        case 8:{
                            $condition.=" AND type=8 ";break;      
                        }
                    }
                    break;
                }
            }
            $connect = Yii::app()->db;
            $sql = "SELECT count(id) as count FROM c_sms_log WHERE 1 ".$condition."";
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();
            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;

            $sql="SELECT * FROM c_sms_log WHERE 1 ".$condition." ORDER BY create_date LIMIT ".$first.",".$row_per_page." "; 
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        }
        public function getDataCardLogSearch($from_date,$to_date,$keyword,$search_type,$page,$row_per_page,$name_search){
            $condition="";
            $from_date=strtotime($from_date);
            if($from_date>0){
                $condition.="AND create_date >".$from_date;
            }
            $to_date = $to_date." 23:59:59";
            $to_date=strtotime($to_date);
            if($to_date>0){
                $condition.=" AND create_date <".$to_date;
            }

            $userId=APaymentRevenue::getUserId($name_search);
            if(!empty($userId)){
                $id= $userId['id'] ; 
                $condition.=" AND userId=".$id; 
            }
            else{
                $condition.="";
            }

            switch($search_type){
                case 3: $condition.=" AND card_seri=".$keyword; break;
                case 4:{
                    $sql="SELECT id FROM c_partner WHERE name='". $keyword."'" ;
                    $command=Yii::app()->db->createCommand($sql);
                    $partnerId=$command->queryRow();
                    if(!empty($partnerId)){
                        $id= $partnerId['id'] ; 
                        $condition.=" AND partnerId='$id'"; 
                    }
                    else{
                        $condition.="";
                    }
                    break ;
                }
                case 5:{
                    /*$sql="SELECT id FROM c_game WHERE title LIKE '%". $keyword."%'" ;
                    $command=Yii::app()->db->createCommand($sql);
                    $ContentId=$command->queryAll();
                    if(!empty($ContentId)){*/
                    $condition.=" AND dataId IN (SELECT id FROM c_game WHERE title LIKE '%". $keyword."%')"; 
                    /*}
                    else{
                    $condition.="";
                    }         */
                }
            }

            $connect = Yii::app()->db;
            $sql = "SELECT count(id) as count FROM c_card_log WHERE 1 ".$condition.""; 
            $command = $connect->createCommand($sql);
            $data_count = $command->queryRow();

            $max_page = ceil(intval($data_count["count"])/$row_per_page);
            $first = ($page - 1)*$row_per_page;

            $sql="SELECT * FROM c_card_log WHERE 1 ".$condition." ORDER BY create_date LIMIT ".$first.",".$row_per_page." ";
            //echo $sql;die;
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $data = $command->queryAll();
            return array($max_page,intval($data_count["count"]),$data);
        }
        public function getPartnerById($id){
            $connect = Yii::app()->db;
            $sql = "SELECT name  FROM c_partner WHERE id =".$id."";
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();
            return $row;

        }
        /* public function GetPartnerIdByName($keyword){
        $sql="SELECT id FROM c_partner WHERE name=". $keyword."" ;
        $command=Yii::app()->db->createCommand($sql);
        $row=$command->queryRow();
        return $row;
        }   */
        public function getGameById($id){
            $connect = Yii::app()->db;
            $sql = "SELECT title  FROM c_game WHERE id =".$id."";
            $command = $connect->createCommand($sql);
            $row = $command->queryRow();
            return $row;
        }
        public function getUserId($id){
            $connect = Yii::app()->db;
            $sql = "SELECT id,username,refCode,email  FROM c_user WHERE id =".$id."";
            $command = $connect->createCommand($sql); 
            $row = $command->queryRow();
            return $row;
        }
    }
?>
